<template>
  <div class="container">
    <div class="box-login" v-if="condition">
      <img class="logo" src="../assets/logo.png" alt />
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item class="username" prop="username">
          <el-input type="text" v-model="form.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="password" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="success" class="left" @click="register">去注册</el-button>
          <div class="btn">
            <el-button size="mini" type="primary" @click="submitForm('form')" class="right">登录</el-button>
            <el-button size="mini" @click="resetForm('form')" class="right">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="box-login" v-else>
      <img class="logo" src="../assets/logo.png" alt />
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item class="username" prop="username">
          <el-input type="text" v-model="form.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="password" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="password" prop="pwd">
          <el-input type="password" v-model="form.pwd" placeholder="请确认密码">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="success" class="left" @click="login">去登录</el-button>
          <div class="btn">
            <el-button size="mini" type="primary" @click="regForm('form')" class="right">注册</el-button>
            <el-button size="mini" @click="resetForm('form')" class="right">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login_api } from "@/api/index.js";
export default {
  data() {
    return {
      condition: true,
      form: {
        username: "",
        password: "",
        pwd: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (!valid) {
          return;
        } else {
          const res = await login_api(this.form);
          console.log(res);
          if (res.status === 200) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            window.sessionStorage.setItem("token", res.data.data.token);
            this.$router.push({ path: "/welcome" });
          } else {
            this.$message.error("登录失败");
          }
        }
      });
    },
    regForm(form) {
      this.$refs[form].validate((valid) => {
        if (!valid) {
          return;
        } else {
        }
      });
    },
    resetForm(form) {
      this.$refs[form].resetFields();
    },
    register() {
      this.condition = false;
    },
    login() {
      this.condition = true;
    },
  },
};
</script>
<style lang="less" scoped>
.container {
  position: relative;
  height: 100%;
  background-color: #2a4b6a;
  .box-login {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    .logo {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      border-radius: 50%;
      background-color: #ccc;
      border: 10px solid #fff;
      box-shadow: 0 0 10px #ccc;
    }
    .login-form {
      padding: 10px;
      .username {
        margin-top: 80px;
        margin-bottom: 15px !important;
      }
      .password {
        margin-bottom: 10px !important;
      }
      .left {
        float: left;
        margin-top: 10px;
      }
      div.btn {
        float: right;
      }
    }
  }
}
</style>
